// pages/personalPage/publish/repeat.js
var config = require('../../../config');
var app = getApp();

Page({
  data: {
    navH: app.globalData.navHeight,
    theme: config.theme,
    checkboxItems: [
      { name: '周一', value: 'day1' },
      { name: '周二', value: 'day2' },
      { name: '周三', value: 'day3' },
      { name: '周四', value: 'day4' },
      { name: '周五', value: 'day5' },
      { name: '周六', value: 'day6' },
      { name: '周日', value: 'day7' }
    ],
    program: null
  },
  onLoad: function (options) {
    if (options.repeat) {
      let items = this.data.checkboxItems;
      items.forEach(v => {
        if (options.repeat.includes(v.value)) {
          v.checked = true;
        } else {
          v.checked = false;
        }
      });
      this.setData({ checkboxItems: items });
    }
    if (options.program) {
      this.setData({ program: options.program });
    }
  },
  onNavBack() {
    // 返回
    wx.navigateBack();
  },
  checkboxChange: function (e) {
    var checkboxItems = this.data.checkboxItems,
      values = e.detail.value;

    for (var i = 0, lenI = checkboxItems.length; i < lenI; ++i) {
      checkboxItems[i].checked = false;

      for (var j = 0, lenJ = values.length; j < lenJ; ++j) {
        if (checkboxItems[i].value == values[j]) {
          checkboxItems[i].checked = true;
          break;
        }
      }
    }

    this.setData({
      checkboxItems: checkboxItems
    });
  },
  goback: function () {
    let checkedItems = [];
    this.data.checkboxItems.forEach(v => {
      v.checked === true && checkedItems.push(v.value);
    });
    if (checkedItems.length == 0) {
      return false;
    }
    let program = JSON.parse(this.data.program);
    program.repeat = checkedItems.join(',');

    wx.redirectTo({
      url: './publish?program=' + JSON.stringify(program)
    });
  }
});
